package com.wzcl.app.dao.repository;

import com.wzcl.app.dao.model.CatalogTask;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface CatalogTaskRepository extends JpaRepository<CatalogTask, Integer> {
    //通过章节id查询章节任务
    @Query(value = "select ct from CatalogTask ct where ct.essayCourseCatalog.id = ?1")
    List<CatalogTask> findAllCatalog(Integer catalogId);

    //通过章节id查询章节任务id
    @Query(value = "select ct.id from CatalogTask ct where ct.essayCourseCatalog.id = ?1")
    List<Integer> findTaskIdByCatalogId(Integer catalogId);

    //查询当前章节下的所有任务id
    @Query(value = "select ct.id from CatalogTask ct where ct.essayCourseCatalog.id = ?1")
    List<Integer> findAllTaskId(Integer catalogId);

    //通过章节id查询章节任务
    @Query(value = "select ct from CatalogTask ct where ct.id = ?1")
    CatalogTask findByTaskId(Integer taskId);

    @Query(value = "update CatalogTask ct set ct.video = ?1, ct.questionList = ?2 where ct.id = ?3")
    @Modifying
    @Transactional
    void updateVideoAndQuestion(String video, String questions, Integer taskId);

    @Query(value = "delete from CatalogTask ct where ct.essayCourseCatalog.id is null")
    @Modifying
    @Transactional
    void deleteByIf();
}